package net.sudot.chess.config;

import net.sudot.chess.business.model.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.Optional;

/**
 * 实体类{@link CreatedBy}及{@link LastModifiedBy}注解值注入
 *
 * @author tangjialin on 2018-05-04.
 */
@EnableJpaAuditing
@Configuration
public class BaseEntityAuditing implements AuditorAware<Long> {
    @Override
    public Optional<Long> getCurrentAuditor() {
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if (principal instanceof User) {
            return Optional.of(((User) principal).getId());
        }
        return Optional.empty();
    }
}
